//
//  HJFansCell.m
//  project
//
//  Created by yefei on 2019/7/24.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJFansCell.h"
#import "HJFansModel.h"
@interface HJFansCell ()

/**
 头像
 */
@property (nonatomic,strong) UIImageView * avator;

/**
 名字
 */
@property (nonatomic,strong) UILabel * name;

/**
 关注按钮
 */
@property (nonatomic,strong) UIButton * attenBtn;

/**
 相互关注标签
 */
//@property (nonatomic,strong) UILabel * focusLabel;

@property (nonatomic,strong) HJFansModel * fansModel;

@property (nonatomic,assign) TableViewCell cellType;

@end
@implementation HJFansCell



-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.avator];
        [self.contentView addSubview:self.name];
        [self.contentView addSubview:self.attenBtn];
        
        [self.avator mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(18);
            make.centerY.equalTo(self.contentView);
            make.size.mas_equalTo(CGSizeMake(40, 40));
            [self.avator rounded:UIRectCornerAllCorners radius:20];
        }];
        
        [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.avator.mas_right).offset(14);
            make.centerY.equalTo(self.avator);
            make.height.mas_equalTo(20);
        }];
        
        [self.attenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView.mas_right).inset(18);
            make.centerY.equalTo(self.contentView);
            make.size.mas_equalTo(CGSizeMake(82, 35));
        }];
    }
    return self;
}

-(UIImageView *)avator {
    if (!_avator) {
        _avator = [UIImageView new];
    }
    return _avator;
}

-(UILabel *)name {
    if (!_name) {
        _name = [UILabel new];
        _name.textColor = [UIColor colorWithHex:0x2a2a2a];
        _name.font = [UIFont fontWithName:PingFangSC_Medium size:18];
    }
    return _name;
}

-(UIButton *)attenBtn {
    if (!_attenBtn) {
        _attenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _attenBtn.titleLabel.font = [UIFont fontWithName:PingFangSC_Medium size:15];
        [_attenBtn setTitle:@"关注" forState:0];
        [_attenBtn setTitle:@"取消关注" forState:UIControlStateSelected];
        [_attenBtn setTitleColor:[UIColor colorWithHex:0xFF2400] forState:0];
        [_attenBtn setTitleColor:[UIColor colorWithHex:0x292929] forState:UIControlStateSelected];
        @weakify(self)
        [[_attenBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self)
            NSString * url;
            if (!self.fansModel.concern) {
                url = [Api stringByAppendingString:ConcernApi];
            }else {
                url = [Api stringByAppendingString:CancelConcernApi];
            }
            [self requestDataWithUrl:url];
        }];
    }
    return _attenBtn;
}
//-(UILabel *)focusLabel {
//    if (!_focusLabel) {
//        _focusLabel = [UILabel new];
//        _focusLabel.textColor =
//    }
//}
-(void)requestDataWithUrl:(NSString *)url {
    @weakify(self)
    [HJHttpTool post:url params:@{@"userId":self.fansModel.user.userId} success:^(HJResponeds *result) {
        @strongify(self)
        self.attenBtn.selected = !self.attenBtn.selected;
        self.fansModel.concern = self.attenBtn.selected;
        if (self.fansModel.concern) {
            [self.attenBtn rounded:UIRectCornerAllCorners radius:2 borderColor:[UIColor colorWithHex:0x292929] borderWidth:1];
        }else {
            [self.attenBtn rounded:UIRectCornerAllCorners radius:2 borderColor:[UIColor colorWithHex:0xff2400] borderWidth:1];
        }
        if (self.attentionDidClickBlock) {
            self.attentionDidClickBlock(self.fansModel);
        }
    } failure:^(id result) {
        [YFHud showError:result];
    }];
}

-(void)loadDataWithModel:(HJFansModel *)model cellType:(TableViewCell)cellType {
    self.cellType = cellType;
    if (cellType ==AttenCell) {
        model.concern = YES;
    }
    self.fansModel = model;
    [self.avator sd_setImageWithURL:[NSURL URLWithString:model.user.avatar]];
    self.name.text = model.user.nickName;
    self.attenBtn.selected = model.concern;
    
    if (model.concern) {
        [self.attenBtn rounded:UIRectCornerAllCorners radius:2 borderColor:[UIColor colorWithHex:0x292929] borderWidth:1];
    }else {
        [self.attenBtn rounded:UIRectCornerAllCorners radius:2 borderColor:[UIColor colorWithHex:0xff2400] borderWidth:1];
    }
}

@end
